# This class is responcible for all interagtions with the generators. 
# It contains the defition of the APIs that can be called by the 
# generators.

class GenerationsController < ApplicationController
  
  


  # POST /generations
  # POST /generations.xml
  def create
    #@generation = Generation.new(params[:generation])

    ## check if there is a generator by this IP
    @generator = Generator.find_or_create_by_ip_address(params[:generator_ip])
    
    # set the name if you don't have it
    if @generator.name.nil? 
      logger.info "Setting generator name for generator at " + params[:generator_ip]
      @generator.name = params[:generator_name]
      @generator.save
    end
    

    # bounce this generator if we've disabled it.
    if @generator.blocked
      logger.warn "Blocked Generator Site Request Denied"
      
      if !params[:generator_ip].nil?
        logger.warn "Blocked Generator IP address: " + params[:generator_ip]
      end
      if !@generator.name.nil? 
        logger.warn "Blocked Generator Name: " + @generator.name
      end
      
        respond_to do |format|
          format.xml{ render :xml => '<error>you are generator non gratta. Please go away</error>', :status => 422 }
        end
    else
      ## this is a valid generator. give it some work.

    ## get the generation this site was last working on.
    logger.info "Getting current generation"
    @generation = @generator.current_generation
    
    if !@generation.nil? && !@generation.complete  
      ## This generator never finished it's job the first time
      ## assign that job again 
      
      logger.warn "Rogue Generator requested site before completing previous Job"
      logger.warn "Rogue Generator Previous Job: " @generation.project.name
      
      if !@generator.ip_address.nil?
        logger.warn "Rogue Generator IP address: " + params[:generator_ip]
      end
      if !@generator.name.nil? 
        logger.warn "Rogue Generator Name: " + @generator.name
      end  
    else
      ## we need a new generation. the last one was finished
      @generation = Scheduler.instance.schedule_next(@generator, logger)
    end
      
      respond_to do |format|
        if !@generation.nil?
          
          logger.info "PAY ATTENTION " + @generation.project.name
          logger.info "generator IP: " + params[:generator_ip]
          logger.info "generator name:" + params[:generator_name]
          
          #@generation = Generation.find(@generation.id, :include => [:project])
          #logger.info @generation.project.name
          
          # save a reference to the current generation in the generator
          # it will be updated the next time the generator runs
          @generator.current_generation_id = @generation.id
          @generator.save
          flash[:notice] = 'Generation was successfully created.'
          logger.info "PAY ATTENTION 2" + @generation.project.name
          format.html { redirect_to(@generation) }
          format.xml  { render :xml => @generation.to_xml(:include => :project, :status => :created, :location => @generation) }
        else
          format.html { render :action => "new" }
          ## return a 501 SERVICE NOT IMPLEMENTED MESSAGE SINCE WE DIDN'T greate a generations
          format.xml  { render :xml => "<NOTHING>There are no sites to generate now. Please come back later</NOTHING>", :status => 501 }

        end
      end
    end
  end



  # PUT /generations/1
  # PUT /generations/1.xml
  def update
    ## check if there is a generator by this IP
    @generator = Generator.find_or_create_by_ip_address(params[:generator_ip])
 
    # bounce this generator if we've disabled it.
    if @generator.blocked
        respond_to do |format|
          format.xml{ render :xml => '<error>you are generator non gratta. Please go away</error>', :status => 422 }
        end
    else
 
      #logger.info 'generatorxml ip' + params[:ip].to_s
      logger.info "Generator ip " + params[:generator_ip].to_s
      action_sent_by_generator =  params[:id].to_s

    
      ## talkin bout my generation
      @generation = @generator.current_generation
      
      unknown_action_from_generator = false
    
      case action_sent_by_generator
      when 'start'
        ## the gerator is starting
        @generation.start_time = Time.now
      when 'end'
        #the generation has ended
        @generation.end_time = Time.now
        @generation.complete = true
      when 'bug_list_download_start'
        #starting a bug list download
        @generation.bug_list_download_start_time = Time.now
      when 'bug_list_download_end'
        # done downloading a bug list
        @generation.bug_list_download_end_time = Time.now
      when 'rendering_pages_start'
        #started rendering the site
        @generation.rendering_pages_start_time = Time.now
      when 'rendering_pages_end'
        # finished rendering the pages.
        @generation.rendering_pages_end_time = Time.now
      when 'rendering_graphs_start'
        #started rendering the graphs
        @generation.rendering_graphs_start_time = Time.now
      when 'rendering_graphs_end'
        # finished rendering the graphs.
        @generation.rendering_graphs_end_time = Time.now

      else
        ## the remote generator said something I don't understand. 
            
        unknown_action_from_generator = true
        respond_to do |format|
          format.html
          format.xml { render :xml => '<error>I don\'t understant what you said.</error>', :status => 422 }
        end
      end
    
      unless unknown_action_from_generator
        if @generation.save  
         respond_to do |format|
            format.html
            format.xml { render :xml => "got it: " + action_sent_by_generator, :status => 200 }
          end
        else
           respond_to do |format|
              format.html
              format.xml { render :xml => '<error>Couldn\' save the update time</error>', :status => 422 }
            end  
        end
        
      end
    end
  end

  # DELETE /generations/1
  # DELETE /generations/1.xml
  def destroy
    @generation = Generation.find(params[:id])
    

    respond_to do |format|
      format.html { redirect_to(generations_url) }
      format.xml  { head :ok }
    end
  end
  
end
